﻿using com.nm.library;
using george.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace com.nm.Helper
{
    public class PageRedirectHelper
    {
        private PageRedirectHelper()
        {
        }

        public void Test(Page pPage)
        {
            if (this.Enabled == false || this.Items.Count == 0) return;
            var url = pPage.Request.Url;
            var r = this.testUrl(url.AbsolutePath);
            if (!string.IsNullOrWhiteSpace(r))
            {
                pPage.Response.Redirect(r, true);
            }
        }

        public void Test(MasterPage pPage)
        {
            if (this.Enabled == false || this.Items.Count == 0) return;
            var url = pPage.Request.Url;
            var r = this.testUrl(url.AbsolutePath);
            if (!string.IsNullOrWhiteSpace(r))
            {
                pPage.Response.Redirect(r, true);
            }
        }

        private string testUrl(string url)
        {
            if (this.Items == null || this.Items.Count <= 0) return string.Empty;
            foreach (var item in this.Items.Where(item => item.IgnoreToTest == false || UserInfo.IsTestUser == false || (item.IgnoreToTest && UserInfo.IsTestUser == false))
                .OrderByDescending(item => item.Priority).ThenBy(item => item.CreateTime))
            {
                var r = item.IsMatch(url);
                if (r != null)
                {
                    return r.Url;
                }
            }
            return string.Empty;
        }

        public int Version { get; set; }
        public bool Enabled { get; set; }
        public List<RedirectRule> Items { get; set; }

        public static PageRedirectHelper Get(string dataPath)
        {
            var txt = myCache.GetFile(dataPath, dataPath);
            return string.IsNullOrWhiteSpace(txt)
                ? new PageRedirectHelper()
                : Newtonsoft.Json.JsonConvert.DeserializeObject<PageRedirectHelper>(txt);
        }

        public static PageRedirectHelper Current
        {
            get { return Get("~/App_Data/PageRedirect.json"); }
        }

        public class RedirectRule
        {
            /// <summary>
            /// 数字大的优先
            /// </summary>
            public int Priority { get; set; }

            /// <summary>
            /// 规则创建时间用来排序
            /// </summary>
            public DateTime CreateTime { get; set; }

            /// <summary>
            /// 匹配条件
            /// </summary>
            public string Match { get; set; }

            /// <summary>
            /// 目标地址，空表示不跳转
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// 子规则
            /// </summary>
            public List<RedirectRule> Items { get; set; }

            /// <summary>
            /// 测试用户是否忽略此规格
            /// </summary>
            public bool IgnoreToTest { get; set; }

            private Regex _regex;

            public RedirectRule IsMatch(string url)
            {
                if (string.IsNullOrWhiteSpace(this.Match)) return null;
                if (_regex == null) _regex = new Regex(this.Match, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (!_regex.IsMatch(url))
                {
                    return null;
                }
                if (this.Items != null && this.Items.Count > 0)
                    foreach (var item in this.Items.OrderByDescending(item => item.Priority).ThenBy(item => item.CreateTime))
                    {
                        var r = item.IsMatch(url);
                        if (r != null)
                            return r;
                    }
                return this;
            }
        }
    }
}